package scalaExec.gui;

import scala.Option;
import scalaExec.Interpreter.GlobalValues;
import scalaExec.scalaLab.clearHistoryAction;
import scalaExec.scalaLab.loadHistoryAction;
import scalaExec.scalaLab.saveHistoryAction;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.net.URL;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.text.DefaultEditorKit;

import scala.tools.nsc.*;
import scala.tools.nsc.util.ScalaClassLoader;


public class scalalabConsole  extends Console {
    public static  scala.tools.nsc.Interpreter  scalaInterpreter;
    scala.tools.nsc.InterpreterSettings  isettings;
    JPopupMenu  consolePopup = new JPopupMenu();
    public  ScalaClassLoader scalaClassLoader;

    static public String compFile;  // Scala compiler file
    static public String libFile;  // Scala rub-time Libraries file
    static public String swingFile;  // Scala Swing library file
    static public String libsFile;   // ScalaLab library file
    static public String javacFile;
    static public String VISADFile;   // the file with the VISAD 3-D Graphics library

      JMenuItem cutJMenuItem = new JMenuItem(new DefaultEditorKit.CutAction());
      JMenuItem copyJMenuItem = new JMenuItem(new DefaultEditorKit.CopyAction());
      JMenuItem pasteJMenuItem = new JMenuItem(new DefaultEditorKit.PasteAction());
      JMenuItem clearHistoryItem = new JMenuItem(new clearHistoryAction());
      JMenuItem loadHistoryItem = new  JMenuItem(new loadHistoryAction());
      JMenuItem saveHistorItem = new JMenuItem(new saveHistoryAction());
      JMenuItem helpItem = new JMenuItem(new ConsoleHelpAction());
      JMenuItem inspectItem = new JMenuItem(new InspectAction());
      JMenuItem displayMatrixItem = new JMenuItem(new DisplayMatrixAction());
      JMenuItem displayOneIndexedMatrixItem = new JMenuItem(new DisplayMatrixActionOneIndexed());
      JMenuItem resetScalaInterpreterItem = new JMenuItem("Reset Scala Interpreter using Scalalab default Imports");
      JMenuItem resetScalaInterpreterEJMLItem = new JMenuItem("Reset Scala Interpreter using Efficient Java Matrix Library of Peter Abeles");
      JMenuItem resetToolboxesScalaInterpreterItem = new JMenuItem("Reset Scala Interpreter using Scalalab default Imports and installed toolboxes");
      JMenuItem clearAllScalaInterpreterItem = new JMenuItem("Reset Scala Interpreter without any default Imports");
      JMenuItem adjustFontItem = new JMenuItem("Adjust Console's font");
      JMenuItem clearConsoleItem = new JMenuItem("Clear Console");
      JMenuItem clearOutputConsoleItem = new JMenuItem("Clear Error and Output Console");
      JMenuItem displayUndockedToolbarItem = new JMenuItem("Display Toolbar Undocked, if not already visible");
   
      JMenuItem transparentErrorMessagesItem=null;
      JMenuItem verboseErrorMessagesItem = null;
      
      JMenuItem clearErrorConsoleItem = new JMenuItem("Clear Error and Output Console");
      
      
     public Settings scalaSettings = new Settings();
      
      public scalalabConsole()
	{
        
        setToolTipText("ScalaLab Console based on a Scala Interpreter: scalaLab's library default imports are used ");
            
      resetToolboxesScalaInterpreterItem.addActionListener(new ActionListener() {
   @Override
            public void actionPerformed(ActionEvent e) {
                interpreterWithAppendedCP(GlobalValues.ScalaSciClassPathComponents);
            }
        });

        displayUndockedToolbarItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            if (GlobalValues.mainToolbarVisible==false)  {
                            JFrame jf = new JFrame("ScalaLab Toolbar");
                            
                            jf.add(GlobalValues.scalalabMainFrame.tabbedToolbarsUnDocked);
                            jf.setSize(600, 200);
                            jf.setLocation(200, 200);
                            jf.setVisible(true);
                        }
         }
        });
        
          resetScalaInterpreterEJMLItem.addActionListener(new ActionListener() {
   @Override
            public void actionPerformed(ActionEvent e) {
                createInterpreterForResetEJML();
            }
        });

          resetScalaInterpreterItem.addActionListener(new ActionListener() {
   @Override
            public void actionPerformed(ActionEvent e) {
                     interpreterWithAppendedCP(GlobalValues.ScalaSciClassPathComponents);
            }
        });

        clearAllScalaInterpreterItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
              createInterpreterForClearAll();
            }
        });
      adjustFontItem.addActionListener(new  ConsoleFontAdjusterAction());
            
  clearConsoleItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
          scalaSciCommands.BasicCommands.cls();
          displayPrompt();
            }
        });

                
        if (GlobalValues.verboseErrorMessages)
            verboseErrorMessagesItem = new JMenuItem("Verbose Error Messages is ON");
        else
            verboseErrorMessagesItem = new JMenuItem("Console-Only Error Messages is ON");

        verboseErrorMessagesItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
           GlobalValues.verboseErrorMessages = !GlobalValues.verboseErrorMessages;
        if (GlobalValues.verboseErrorMessages)
            verboseErrorMessagesItem.setText("Verbose Error Messages is ON");
        else
            verboseErrorMessagesItem.setText("Console-Only Error Messages is ON");
        makePopup();
        }
        });
        verboseErrorMessagesItem.setToolTipText("Switches between displaying messages to the Console only, and displaying them also in error windows");

      if (GlobalValues.transparentErrorMessages)
        transparentErrorMessagesItem = new JMenuItem("Transparent Error Messages is ON");
      else
          transparentErrorMessagesItem = new JMenuItem("Opaque Error Messages is ON");
      transparentErrorMessagesItem.addActionListener(new ActionListener() {
         @Override
            public void actionPerformed(ActionEvent e) {
              GlobalValues.transparentErrorMessages = !GlobalValues.transparentErrorMessages;
              if (GlobalValues.transparentErrorMessages)
        transparentErrorMessagesItem.setText("Transparent Error Messages is ON");
      else
          transparentErrorMessagesItem.setText("Opaque Error Messages is ON");
        makePopup();
         }
        });
        transparentErrorMessagesItem.setToolTipText("Controls the type of the error window i.e.  opaque vs transparent");

        // sterg-SOS: createInterpreter();
            //bindSettings();

            makePopup();
            this.add(consolePopup);
            this.addMouseListener(new MouseAdapterForConsole());   // handles right mouse clicks
            
            this.setBackground(Color.WHITE);
            this.setFont(new Font("Lucida",  Font.PLAIN, 14));
            commandNo = 0;
            previousCommands = new Vector(10, 10);
            lineStart = getText().length() + 1;
		
            keyHandler = new ConsoleKeyHandler();
		
            addKeyListener(keyHandler);
            
        clearErrorConsoleItem.setAccelerator(KeyStroke.getKeyStroke("F5"));
            
            }	
       
  
     URL  jarPathOfClass(String className) {
        try {
            return Class.forName(className).getProtectionDomain().getCodeSource().getLocation();
        } catch (ClassNotFoundException ex) {
           System.out.println("error in jarPathOfClass"+className+")");
           ex.printStackTrace();
           return null;
        }
}
      
     
/** Create a new interpreter. */
      public void createInterpreter()  {
          JFrame initFrame = new JFrame("Initializing Scala scripting engine ... Please wait ... ");
          initFrame.setSize(800, 100);
          Font myFont = new Font("Arial", Font.BOLD, 20);
          JLabel myLabel = new JLabel("Initializing Scala scripting engine ...  Please wait a few seconds ...");
          myLabel.setFont(myFont);
          initFrame.add(myLabel);
          initFrame.setLocation(200, 300);
          initFrame.setVisible(true);

          String jarFile = GlobalValues.jarFilePath;
         if (GlobalValues.hostIsUnix) {   
       compFile = jarPathOfClass("scala.tools.nsc.ScriptRunner").toString().replace("file:/", "/");
       libFile =  jarPathOfClass("scala.ScalaObject").toString().replace("file:/","/");
       swingFile = jarPathOfClass("scala.swing.Swing").toString().replace("file:/", "/");
       libsFile = jarPathOfClass("JSci.maths.wavelet.Cascades").toString().replace("file:/", "/");
       javacFile = jarPathOfClass("com.sun.tools.javac.Main").toString().replace("file:/", "/");
       VISADFile = jarPathOfClass("visad.Action").toString().replace("file:/", "/");
                    }
         else {
       compFile = jarPathOfClass("scala.tools.nsc.ScriptRunner").toString().replace("file:/", "");
       libFile =  jarPathOfClass("scala.ScalaObject").toString().replace("file:/","");
       swingFile = jarPathOfClass("scala.swing.Swing").toString().replace("file:/", "");
       libsFile = jarPathOfClass("JSci.maths.wavelet.Cascades").toString().replace("file:/", "");
      javacFile = jarPathOfClass("com.sun.tools.javac.Main").toString().replace("file:/", "");
      VISADFile = jarPathOfClass("visad.Action").toString().replace("file:/", "");
                  }   

          
          // System.out.println("jarFile = "+jarFile+ " compFile = "+compFile+" libFile = "+libFile+" swingFile = "+swingFile);
         //  System.out.println("Basic math libraries file and JFreeChart lib = "+libsFile);
          // pass ScalaSci classpath components to the compiler's settings
            scalaSettings = new Settings();
          //scalaSettings.usejavacp().tryToSetProperty( );
          scalaSettings.classpath().append(jarFile);
          scalaSettings.classpath().append(libsFile);
          scalaSettings.classpath().append(compFile);
          scalaSettings.classpath().append(libFile);
          scalaSettings.classpath().append(swingFile);
          scalaSettings.classpath().append(VISADFile);
  
          GlobalValues.extensionClassLoader.extendClassPath(jarFile);
          GlobalValues.extensionClassLoader.extendClassPath(libsFile);
          GlobalValues.extensionClassLoader.extendClassPath(compFile);
          GlobalValues.extensionClassLoader.extendClassPath(libFile);
          GlobalValues.extensionClassLoader.extendClassPath(swingFile);
          GlobalValues.extensionClassLoader.extendClassPath(VISADFile);
          
          Vector classpath = GlobalValues.ScalaSciClassPathComponents;
           for (int k=0; k<classpath.size(); k++) {
              String clsp = (String)classpath.elementAt(k);
              scalaSettings.classpath().append(clsp.trim());  
          }
  
          GlobalValues.globalInterpreter = scalaInterpreter;
          
          scalaInterpreter =  new  scala.tools.nsc.Interpreter(scalaSettings);
          scalaInterpreter.setContextClassLoader();
          /*scalaClassLoader = scalaInterpreter.getInterpreterClassLoader();
          Thread t = Thread.currentThread();
          t.setContextClassLoader((ClassLoader) scalaClassLoader);   // set the context class loader to the ScalaClassLoader
*/
          GlobalValues.scalalabMainFrame.tabbedToolbars.setTitleAt(2,  "Mat");
          
          scalaInterpreter.interpret(GlobalValues.basicImportsScala);   // interpret the basic imports
          scalaInterpreter.interpret(GlobalValues.scalaPlusImports);   // interpret the additional imports
        //  scalaInterpreter.interpret(GlobalValues.implicitImportsScala);
        
          
          //scalaInterpreter.interpret(GlobalValues.jplotsImports);
         
          GlobalValues.globalInterpreter = scalaInterpreter;
         
          
          initFrame.dispose();
      }
     
/** Create a new interpreter. */
      public void createInterpreterForReset()  {

          scalaSettings = new Settings();
          String jarFile = GlobalValues.jarFilePath;
          
         if (GlobalValues.hostIsUnix) {
       compFile = jarPathOfClass("scala.tools.nsc.ScriptRunner").toString().replace("file:/", "/");
       libFile =  jarPathOfClass("scala.ScalaObject").toString().replace("file:/","/");
       swingFile = jarPathOfClass("scala.swing.Swing").toString().replace("file:/", "/");
       libsFile = jarPathOfClass("").toString().replace("file:/", "/");
       javacFile = jarPathOfClass("com.sun.tools.javac.Main").toString().replace("file:/", "/");
       VISADFile = jarPathOfClass("visad.Action").toString().replace("file:/", "/");
                  }
         else {
       compFile = jarPathOfClass("scala.tools.nsc.ScriptRunner").toString().replace("file:/", "");
       libFile =  jarPathOfClass("scala.ScalaObject").toString().replace("file:/","");
       swingFile = jarPathOfClass("scala.swing.Swing").toString().replace("file:/", "");
       libsFile = jarPathOfClass("JSci.maths.wavelet.Cascades").toString().replace("file:/", "");
       javacFile = jarPathOfClass("com.sun.tools.javac.Main").toString().replace("file:/", "");
       VISADFile = jarPathOfClass("visad.Action").toString().replace("file:/", "/");
       VISADFile = jarPathOfClass("visad.Action").toString().replace("file:/", "");
         }
          
          System.out.println("jarFile = "+jarFile+ " compFile = "+compFile+" libFile = "+libFile+" swingFile = "+swingFile);
          System.out.println("Basic math libraries file and JFreeChart lib = "+libsFile);
          
          scalaSettings = new Settings();
          scalaSettings.classpath().append(jarFile);
          scalaSettings.classpath().append(libsFile);
          scalaSettings.classpath().append(compFile);
          scalaSettings.classpath().append(libFile);
          scalaSettings.classpath().append(swingFile);
          scalaSettings.classpath().append(VISADFile);
    
          GlobalValues.extensionClassLoader.extendClassPath(jarFile);
          GlobalValues.extensionClassLoader.extendClassPath(libsFile);
          GlobalValues.extensionClassLoader.extendClassPath(compFile);
          GlobalValues.extensionClassLoader.extendClassPath(libFile);
          GlobalValues.extensionClassLoader.extendClassPath(swingFile);
          GlobalValues.extensionClassLoader.extendClassPath(VISADFile);
          
  // pass ScalaSci classpath components to the compiler's settings
          Vector classpath = GlobalValues.ScalaSciClassPathComponents;
           for (int k=0; k<classpath.size(); k++) {
              String clsp = (String)classpath.elementAt(k);
              scalaSettings.classpath().append(clsp.trim());  
          }
         
          scalaInterpreter =  new  scala.tools.nsc.Interpreter(scalaSettings);
          scalaInterpreter.setContextClassLoader();
          
          /*scalaClassLoader = scalaInterpreter.getInterpreterClassLoader();
          Thread t = Thread.currentThread();
          t.setContextClassLoader((ClassLoader) scalaClassLoader);   // set the context class loader to the ScalaClassLoader
*/
          
          GlobalValues.scalalabMainFrame.tabbedToolbars.setTitleAt(2,  "Mat");
          
          scalaInterpreter.interpret(GlobalValues.basicImportsScala);   // interpret the basic imports
          scalaInterpreter.interpret(GlobalValues.scalaPlusImports);   // interpret the additional imports
          //scalaInterpreter.interpret(GlobalValues.implicitImportsScala);
          GlobalValues.globalInterpreter = scalaInterpreter;
          
          GlobalValues.autoCompletionWorkspace = new AutoCompletionWorkspace();  
          GlobalValues.autoCompletionWorkspace.scanVars=new Vector();
          scalaExec.gui.watchScalaWorkspace.displayScalaSciBinding(scalaExec.scalaLab.scalaLab.variablesWorkSpacePanel);
          
          scalaExec.scalaLab.scalaLab.outerPane.revalidate();
          scalaSciCommands.BasicCommands.cls();
          GlobalValues.scalalabMainFrame.tabbedToolbars.setTitleAt(2,  "Mat");
          GlobalValues.toolboxUsed = GlobalValues.JAMAMat;
          
          
          displayPrompt(); 
          }

      /** Create a new interpreter. */
      public void createInterpreterForResetEJML()  {

          scalaSettings = new Settings();
          String jarFile = GlobalValues.jarFilePath;
 
          if (GlobalValues.hostIsUnix) {
       compFile = jarPathOfClass("scala.tools.nsc.ScriptRunner").toString().replace("file:/", "/");
       libFile =  jarPathOfClass("scala.ScalaObject").toString().replace("file:/","/");
       swingFile = jarPathOfClass("scala.swing.Swing").toString().replace("file:/", "/");
       libsFile = jarPathOfClass("JSci.maths.wavelet.Cascades").toString().replace("file:/", "/");
       javacFile = jarPathOfClass("com.sun.tools.javac.Main").toString().replace("file:/", "/");
       VISADFile = jarPathOfClass("visad.Action").toString().replace("file:/", "/");
                           }
         else {
       compFile = jarPathOfClass("scala.tools.nsc.ScriptRunner").toString().replace("file:/", "");
       libFile =  jarPathOfClass("scala.ScalaObject").toString().replace("file:/","");
       swingFile = jarPathOfClass("scala.swing.Swing").toString().replace("file:/", "");
       libsFile = jarPathOfClass("JSci.maths.wavelet.Cascades").toString().replace("file:/", "");
       javacFile = jarPathOfClass("com.sun.tools.javac.Main").toString().replace("file:/", "");
       VISADFile = jarPathOfClass("visad.Action").toString().replace("file:/", "");
           }
          
  //        System.out.println("jarFile = "+jarFile+ " compFile = "+compFile+" libFile = "+libFile+" swingFile = "+swingFile);
   //       System.out.println("Basic math libraries file and JFreeChart lib = "+libsFile);
          Vector classpath = GlobalValues.ScalaSciClassPathComponents;
           for (int k=0; k<classpath.size(); k++) {
              String clsp = (String)classpath.elementAt(k);
              scalaSettings.classpath().append(clsp.trim());  
          }
      
          scalaSettings = new Settings();
          scalaSettings.classpath().append(jarFile);
          scalaSettings.classpath().append(libsFile);
          scalaSettings.classpath().append(compFile);
          scalaSettings.classpath().append(libFile);
          scalaSettings.classpath().append(swingFile);
          scalaSettings.classpath().append(VISADFile);
         
          GlobalValues.extensionClassLoader.extendClassPath(jarFile);
          GlobalValues.extensionClassLoader.extendClassPath(libsFile);
          GlobalValues.extensionClassLoader.extendClassPath(compFile);
          GlobalValues.extensionClassLoader.extendClassPath(libFile);
          GlobalValues.extensionClassLoader.extendClassPath(swingFile);
          GlobalValues.extensionClassLoader.extendClassPath(VISADFile);
          
          javacFile = jarPathOfClass("com.sun.tools.javac.Main").toString().replace("file:/", "/");
         System.out.println("javacFile  = "+ javacFile);
         
          scalaInterpreter =  new  scala.tools.nsc.Interpreter(scalaSettings);
          scalaInterpreter.setContextClassLoader();
          
          /*scalaClassLoader = scalaInterpreter.getInterpreterClassLoader();
          Thread t = Thread.currentThread();
          t.setContextClassLoader((ClassLoader) scalaClassLoader);   // set the context class loader to the ScalaClassLoader
*/
          scalaInterpreter.interpret(GlobalValues.basicImportsEJMLScala);   // interpret the basic imports
          scalaInterpreter.interpret(GlobalValues.scalaPlusEJMLImports);   // interpret the additional imports
          //scalaInterpreter.interpret(GlobalValues.implicitImportsScala);
        
          //scalaInterpreter.interpret(GlobalValues.jplotsImports);
          
          GlobalValues.globalInterpreter = scalaInterpreter;
          
          
          GlobalValues.autoCompletionWorkspace = new AutoCompletionWorkspace();  
          GlobalValues.autoCompletionWorkspace.scanVars=new Vector();
          scalaExec.gui.watchScalaWorkspace.displayScalaSciBinding(scalaExec.scalaLab.scalaLab.variablesWorkSpacePanel);
          
          scalaExec.scalaLab.scalaLab.outerPane.revalidate();
          scalaSciCommands.BasicCommands.cls();
          //GlobalValues.consoleOutputWindow.output.setText("");
          GlobalValues.scalalabMainFrame.tabbedToolbars.setTitleAt(2,  "EJML-Mat");
          GlobalValues.toolboxUsed = GlobalValues.EJMLMat;
          displayPrompt(); 
          }

/** Create a new interpreter. */
      public void createInterpreterForClearAll()  {

          scalaSettings = new Settings();
          String jarFile = GlobalValues.jarFilePath;
          
       libsFile = jarPathOfClass("JSci.maths.wavelet.Cascades").toString().replace("file:/", "/");
         
         System.out.println("Basic math libraries file and JFreeChart lib = "+libsFile);
          scalaSettings = new Settings();
          scalaSettings.classpath().append(jarFile);
          scalaSettings.classpath().append(libsFile);
          scalaSettings.classpath().append(compFile);
          scalaSettings.classpath().append(libFile);
          scalaSettings.classpath().append(swingFile);
  
          GlobalValues.extensionClassLoader.extendClassPath(jarFile);
          GlobalValues.extensionClassLoader.extendClassPath(libsFile);
          GlobalValues.extensionClassLoader.extendClassPath(compFile);
          GlobalValues.extensionClassLoader.extendClassPath(libFile);
          GlobalValues.extensionClassLoader.extendClassPath(swingFile);
          
          // pass ScalaSci classpath components to the compiler's settings
          Vector classpath = GlobalValues.ScalaSciClassPathComponents;
           for (int k=0; k<classpath.size(); k++) {
              String clsp = (String)classpath.elementAt(k);
              scalaSettings.classpath().append(clsp.trim());  
          }
  
          scalaInterpreter =  new  scala.tools.nsc.Interpreter(scalaSettings);
          scalaInterpreter.setContextClassLoader();
          
          GlobalValues.scalalabMainFrame.tabbedToolbars.setTitleAt(2,  "Mat");
          
         GlobalValues.autoCompletionWorkspace = new AutoCompletionWorkspace();
         GlobalValues.autoCompletionWorkspace.scanVars=new Vector();
     
         scalaExec.gui.watchScalaWorkspace.displayScalaSciBinding(scalaExec.scalaLab.scalaLab.variablesWorkSpacePanel);
         scalaExec.scalaLab.scalaLab.outerPane.revalidate();
          scalaSciCommands.BasicCommands.cls();
          displayPrompt();
          }
     

      // append a single path specification to the classpath
  public void  interpreterWithAppendedCP(String classpath) {
      classpath = classpath.trim();   
      scalaSettings = new Settings();
      String jarFile = GlobalValues.jarFilePath;
      
      if (GlobalValues.hostIsUnix) {
       compFile = jarPathOfClass("scala.tools.nsc.ScriptRunner").toString().replace("file:/", "/");
       libFile =  jarPathOfClass("scala.ScalaObject").toString().replace("file:/","/");
       swingFile = jarPathOfClass("scala.swing.Swing").toString().replace("file:/", "/");
       libsFile = jarPathOfClass("JSci.maths.wavelet.Cascades").toString().replace("file:/", "/");
       javacFile = jarPathOfClass("com.sun.tools.javac.Main").toString().replace("file:/", "/");
       VISADFile = jarPathOfClass("visad.Action").toString().replace("file:/", "/");
                           }
         else {
       compFile = jarPathOfClass("scala.tools.nsc.ScriptRunner").toString().replace("file:/", "");
       libFile =  jarPathOfClass("scala.ScalaObject").toString().replace("file:/","");
       swingFile = jarPathOfClass("scala.swing.Swing").toString().replace("file:/", "");
       libsFile = jarPathOfClass("JSci.maths.wavelet.Cascades").toString().replace("file:/", "");
       javacFile = jarPathOfClass("com.sun.tools.javac.Main").toString().replace("file:/", "");
       VISADFile = jarPathOfClass("visad.Action").toString().replace("file:/", "");
           }
          
  //    System.out.println("jarFile = "+jarFile+ " compFile = "+compFile+" libFile = "+libFile+" swingFile = "+swingFile);
  //    System.out.println("javacFile  = "+ javacFile);
 //     System.out.println("Basic math libraries file and JFreeChart lib = "+libsFile);
   
      scalaSettings = new Settings();
          //scalaSettings.usejavacp().tryToSetProperty( );
       scalaSettings.classpath().append(jarFile);
       scalaSettings.classpath().append(libsFile);
       scalaSettings.classpath().append(compFile);
       scalaSettings.classpath().append(libFile);
       scalaSettings.classpath().append(swingFile);
       scalaSettings.classpath().append(VISADFile);

       GlobalValues.extensionClassLoader.extendClassPath(jarFile);
          GlobalValues.extensionClassLoader.extendClassPath(libsFile);
          GlobalValues.extensionClassLoader.extendClassPath(compFile);
          GlobalValues.extensionClassLoader.extendClassPath(libFile);
          GlobalValues.extensionClassLoader.extendClassPath(swingFile);
          GlobalValues.extensionClassLoader.extendClassPath(VISADFile);
          
          
      // scalaSettings.classpath().append(compFile);
         // scalaSettings.classpath().append(libFile);
         // scalaSettings.classpath().append(swingFile);
         scalaSettings.classpath().append(classpath);
  
         Vector classpathScalaSci = GlobalValues.ScalaSciClassPathComponents;
          for (int k=0; k<classpathScalaSci.size(); k++) {
              String clsp = (String)classpathScalaSci.elementAt(k);
              scalaSettings.classpath().append(clsp.trim());  
          }
  
          scalaInterpreter =  new  scala.tools.nsc.Interpreter(scalaSettings);
          scalaInterpreter.setContextClassLoader();
              
      
      GlobalValues.scalalabMainFrame.tabbedToolbars.setTitleAt(2,  "Mat");
      
          /*scalaClassLoader = scalaInterpreter.getInterpreterClassLoader();
          Thread t = Thread.currentThread();
          t.setContextClassLoader((ClassLoader) scalaClassLoader);   // set the context class loader to the ScalaClassLoader
*/
          scalaInterpreter.interpret(GlobalValues.basicImportsScala);   // interpret the basic imports
          scalaInterpreter.interpret(GlobalValues.scalaPlusImports);   // interpret the additional imports
          //scalaInterpreter.interpret(GlobalValues.implicitImportsScala);
        
          
          
          // sterg-SOS GlobalValues.consoleOutputWindow.output.setText("");
          scalaExec.scalaLab.scalaLab.outerPane.revalidate();
          scalaSciCommands.BasicCommands.cls();
          displayPrompt();

          if (GlobalValues.ScalaSciUserPaths.contains(classpath)==false)
              GlobalValues.ScalaSciUserPaths.add(classpath);
         GlobalValues.globalInterpreter = scalaInterpreter;
                  
  }
  

  // append an array of classpath specifications to the classpath
  public void  interpreterWithAppendedCP(String [] classpath) {
          scalaSettings = new Settings();
          String jarFile = GlobalValues.jarFilePath;
          
          if (GlobalValues.hostIsUnix) {
       compFile = jarPathOfClass("scala.tools.nsc.ScriptRunner").toString().replace("file:/", "/");
       libFile =  jarPathOfClass("scala.ScalaObject").toString().replace("file:/","/");
       swingFile = jarPathOfClass("scala.swing.Swing").toString().replace("file:/", "/");
       libsFile = jarPathOfClass("JSci.maths.wavelet.Cascades").toString().replace("file:/", "/");
       javacFile = jarPathOfClass("com.sun.tools.javac.Main").toString().replace("file:/", "/");
       VISADFile = jarPathOfClass("visad.Action").toString().replace("file:/", "/");
                           }
         else {
       compFile = jarPathOfClass("scala.tools.nsc.ScriptRunner").toString().replace("file:/", "");
       libFile =  jarPathOfClass("scala.ScalaObject").toString().replace("file:/","");
       swingFile = jarPathOfClass("scala.swing.Swing").toString().replace("file:/", "");
       libsFile = jarPathOfClass("JSci.maths.wavelet.Cascades").toString().replace("file:/", "");
       javacFile = jarPathOfClass("com.sun.tools.javac.Main").toString().replace("file:/", "");
       VISADFile = jarPathOfClass("visad.Action").toString().replace("file:/", "");
           }
          
          //System.out.println("jarFile = "+jarFile+ " compFile = "+compFile+" libFile = "+libFile+" swingFile = "+swingFile);
         // System.out.println("Basic math libraries file and JFreeChart lib = "+libsFile);
         // System.out.println("javacFile  = "+ javacFile);
         
          scalaSettings.classpath().append(compFile);
          scalaSettings.classpath().append(libFile);
          scalaSettings.classpath().append(swingFile);
          scalaSettings.classpath().append(jarFile);
          scalaSettings.classpath().append(libsFile);
          scalaSettings.classpath().append(VISADFile);
      
          for (String clsp:classpath)  
              scalaSettings.classpath().append(clsp.trim());
           
          Vector classpathScalaSci = GlobalValues.ScalaSciClassPathComponents;
           for (int k=0; k<classpathScalaSci.size(); k++) {
              String clsp = (String)classpathScalaSci.elementAt(k);
              scalaSettings.classpath().append(clsp.trim());  
          }
  
          GlobalValues.extensionClassLoader.extendClassPath(jarFile);
          GlobalValues.extensionClassLoader.extendClassPath(libsFile);
          GlobalValues.extensionClassLoader.extendClassPath(compFile);
          GlobalValues.extensionClassLoader.extendClassPath(libFile);
          GlobalValues.extensionClassLoader.extendClassPath(swingFile);
          GlobalValues.extensionClassLoader.extendClassPath(VISADFile);
          
          scalaInterpreter =  new  scala.tools.nsc.Interpreter(scalaSettings);
          scalaInterpreter.setContextClassLoader();
          
          GlobalValues.scalalabMainFrame.tabbedToolbars.setTitleAt(2,  "Mat");
          
          /*scalaClassLoader = scalaInterpreter.getInterpreterClassLoader();
          Thread t = Thread.currentThread();
          t.setContextClassLoader((ClassLoader) scalaClassLoader);   // set the context class loader to the ScalaClassLoader
*/
          scalaInterpreter.interpret(GlobalValues.basicImportsScala);   // interpret the basic imports
          scalaInterpreter.interpret(GlobalValues.scalaPlusImports);   // interpret the additional imports
          //scalaInterpreter.interpret(GlobalValues.implicitImportsScala);
          
          
          scalaExec.scalaLab.scalaLab.outerPane.revalidate();
          scalaSciCommands.BasicCommands.cls();
          displayPrompt();
          // sterg-SOS GlobalValues.consoleOutputWindow.output.setText("");
          
          for (String clsp:classpath) {
              if (GlobalValues.ScalaSciUserPaths.contains(clsp)==false)
              GlobalValues.ScalaSciUserPaths.add(clsp.trim());
          }

          GlobalValues.globalInterpreter = scalaInterpreter;
          
  }
    // append a Vector of classpath specifications to the classpath
  public void  interpreterWithAppendedCP(Vector classpath) {
          scalaSettings = new Settings();
          String jarFile = GlobalValues.jarFilePath;
          System.out.println("jarFile = "+jarFile);
         
          if (GlobalValues.hostIsUnix) {
       compFile = jarPathOfClass("scala.tools.nsc.ScriptRunner").toString().replace("file:/", "/");
       libFile =  jarPathOfClass("scala.ScalaObject").toString().replace("file:/","/");
       swingFile = jarPathOfClass("scala.swing.Swing").toString().replace("file:/", "/");
       libsFile = jarPathOfClass("JSci.maths.wavelet.Cascades").toString().replace("file:/", "/");
       javacFile = jarPathOfClass("com.sun.tools.javac.Main").toString().replace("file:/", "/");
       VISADFile = jarPathOfClass("visad.Action").toString().replace("file:/", "/");
                           }
         else {
       compFile = jarPathOfClass("scala.tools.nsc.ScriptRunner").toString().replace("file:/", "");
       libFile =  jarPathOfClass("scala.ScalaObject").toString().replace("file:/","");
       swingFile = jarPathOfClass("scala.swing.Swing").toString().replace("file:/", "");
       libsFile = jarPathOfClass("JSci.maths.wavelet.Cascades").toString().replace("file:/", "");
       javacFile = jarPathOfClass("com.sun.tools.javac.Main").toString().replace("file:/", "");
       VISADFile = jarPathOfClass("visad.Action").toString().replace("file:/", "");
           }
          
          System.out.println("jarFile = "+jarFile+ " compFile = "+compFile+" libFile = "+libFile+" swingFile = "+swingFile);
          System.out.println("javacFile  = "+ javacFile);
          System.out.println("Basic math libraries file and JFreeChart lib = "+libsFile);
          
          scalaSettings = new Settings();
          scalaSettings.classpath().append(jarFile);
          scalaSettings.classpath().append(libsFile);
          
         scalaSettings.classpath().append(compFile);
         scalaSettings.classpath().append(libFile);
         scalaSettings.classpath().append(swingFile);
         scalaSettings.classpath().append(VISADFile);
 
          
          for (int k=0; k<classpath.size(); k++) {
              String clsp = (String)classpath.elementAt(k);
              scalaSettings.classpath().append(clsp.trim());  
          }
          
         GlobalValues.extensionClassLoader.extendClassPath(jarFile);
          GlobalValues.extensionClassLoader.extendClassPath(libsFile);
          GlobalValues.extensionClassLoader.extendClassPath(compFile);
          GlobalValues.extensionClassLoader.extendClassPath(libFile);
          GlobalValues.extensionClassLoader.extendClassPath(swingFile);
//          GlobalValues.extensionClassLoader.extendClassPath(VISADFile);
          
         scalaInterpreter =  new  scala.tools.nsc.Interpreter(scalaSettings);
          scalaInterpreter.setContextClassLoader();
          
          GlobalValues.scalalabMainFrame.tabbedToolbars.setTitleAt(2,  "Mat");
          
          /*scalaClassLoader = scalaInterpreter.getInterpreterClassLoader();
          Thread t = Thread.currentThread();
          t.setContextClassLoader((ClassLoader) scalaClassLoader);   // set the context class loader to the ScalaClassLoader
*/
          scalaInterpreter.interpret(GlobalValues.basicImportsScala);   // interpret the basic imports
          scalaInterpreter.interpret(GlobalValues.scalaPlusImports);   // interpret the additional imports
        //  scalaInterpreter.interpret(GlobalValues.implicitImportsScala);
          
          
          scalaExec.scalaLab.scalaLab.outerPane.revalidate();
          scalaSciCommands.BasicCommands.cls();
          displayPrompt();
          // sterg-SOS GlobalValues.consoleOutputWindow.output.setText("");
          
          for (int k=0; k<classpath.size(); k++) {
              String clsp = (String)classpath.elementAt(k);
              if (GlobalValues.ScalaSciUserPaths.contains(clsp)==false)
                GlobalValues.ScalaSciUserPaths.add(clsp.toString().trim());
            }
          GlobalValues.globalInterpreter = scalaInterpreter;
          
     }
  

  public void Completion(String line) {
    //  boolean completion = scala.tools.nsc.interpreter.Completion$.MODULE$.isValidCompletion(line);
      System.out.println("completion = ");
  }
 
  
      /** Bind the settings so that evaluated code can modiy them */
      public void bindSettings() {
          isettings = scalaInterpreter.isettings();
          scalaInterpreter.bind( "settings",    "scala.tools.nsc.InterpreterSettings",     isettings);
      }

    public void interpretLine(String theLine)
    { 
        theLine = theLine.trim();
        scalaExec.scalaLab.scalaLabUtils.controlOutput(theLine);    // controls the verbose level of the output depending on ending command with semicolon
        InterpreterResults.Result  rs = scalaInterpreter.interpret(theLine);
              
         commandNo = previousCommands.size();
        adjustConsoleWindow();
    }
    
   public void adjustConsoleWindow() {
       // if autoAdjustConsoleOut flag is set we 
        if (GlobalValues.notOverlapTheConsoleOutput) {  
            int xSS = GlobalValues.scalalabMainFrame.getSize().width;
            int ySS = GlobalValues.scalalabMainFrame.getSize().height;
            int xSL = GlobalValues.scalalabMainFrame.getLocation().x;
            int ySL = GlobalValues.scalalabMainFrame.getLocation().y;
            int xO = GlobalValues.consoleOutputWindow.consoleFrame.getLocation().x;
            int yO = GlobalValues.consoleOutputWindow.consoleFrame.getLocation().y;
            if (ySL+ySS > yO)  // overlap across y-axis
              if (  xSL+xSS > xO )      
            {  
                GlobalValues.consoleOutputWindow.readjustScalaLabWindow();
            }
        }    
    }

      /**Interpret the current line*/
	public void interpretLine()
	{
    
		//get the text on the current line
        String text = getText();
        String inputString = text.substring(text.lastIndexOf(GlobalValues.scalalabPromptString) + 2, text.length());
        inputString = inputString.trim();
        InterpreterResults.Result rs;
     
        
        
        /* exit application */
        if (inputString.equals("quit") || inputString.equals("exit"))
		{
			GlobalValues.scalalabMainFrame.closeGUI();   // call the main close() routine
		}
        else {
        if (!inputString.equals(""))
        {
                 boolean isFastScript = FastScriptingMode.isFastScripting(inputString);
                 if (isFastScript)
                    inputString = FastScriptingMode.transformScriptToFast(inputString);
                 
                  rs =  scalaInterpreter.interpret(inputString);

                 commandNo = previousCommands.size();
                if (rs.toString().contains("Error")) {}  // nothing to display at the console window for the error
                else  {  // not Error in result
     
                     if (inputString.length()>0)
                    previousCommands.addElement(inputString);
        
                     
                     // TODO-Sterg: implement better result displaying control
          //       scalaExec.scalaLab.scalaLabUtils.controlOutput(inputString);    // controls the verbose level of the output depending on ending command with semicolon
                    
                GlobalValues.scalalabMainFrame.updateHistoryWindow();
            
        //        if (GlobalValues.displayScalaSciBindingGlobal && GlobalValues.displayCommandScalaSciBinding) {
                    scalaExec.scalaLab.scalaLab.outerPane.revalidate();
                    
                scalaExec.gui.watchScalaWorkspace.displayScalaSciBinding(scalaExec.scalaLab.scalaLab.variablesWorkSpacePanel);
                String answerString = rs.toString();
                 GlobalValues.scalalabMainFrame.scalalabConsole.displayText(answerString);
                 GlobalValues.consoleOutputWindow.outputPane.scrollRectToVisible(null);
                 GlobalValues.consoleOutputWindow.outputPane.revalidate();
               
                    //}  // verboseOutput
                
                }  // not Error in result
             }  // not "" (i.e. null) command
         } 
              adjustConsoleWindow();

        }

        
  /**Interpret the current line appending default imports */
	public void interpretLineDefaultImports()
	{

		//get the text on the current line
        String text = getText();
        String inputString = text.substring(text.lastIndexOf(GlobalValues.scalalabPromptString) + 2, text.length());
        inputString = inputString.trim();
        
   		/* exit application */
        if (inputString.equals("quit") || inputString.equals("exit"))
		{
			GlobalValues.scalalabMainFrame.closeGUI();   // call the main close() routine
		}

     append("\n");
		
        if (!inputString.equals(""))
        {
                if (inputString.length()>0)
                    previousCommands.addElement(inputString);

  inputString = GlobalValues.basicImportsScala+"\n"+GlobalValues.scalaPlusImports+"\n"+inputString;

            scalaExec.scalaLab.scalaLabUtils.controlOutput(inputString);    // controls the verbose level of the output depending on ending command with semicolon
            InterpreterResults.Result  rs = scalaInterpreter.interpret(inputString);
            commandNo = previousCommands.size();
           String answerString = rs.toString();
           GlobalValues.scalalabMainFrame.scalalabConsole.displayText(answerString);
           GlobalValues.lastResultOutputArea.setText(answerString);
             }
      }
     


  /**Interpret the current line appending default imports */
	public void interpretLineWithoutDefaultImports()
	{

		//get the text on the current line
        String text = getText();
        String inputString = text.substring(text.lastIndexOf(GlobalValues.scalalabPromptString) + 2, text.length());
        inputString = inputString.trim();
        
   		/* exit application */
        if (inputString.equals("quit") || inputString.equals("exit"))
		{
			GlobalValues.scalalabMainFrame.closeGUI();   // call the main close() routine
		}

     append("\n");

        if (!inputString.equals(""))
        {
                if (inputString.trim().length()>0)
                    previousCommands.addElement(inputString);

            scalaExec.scalaLab.scalaLabUtils.controlOutput(inputString);    // controls the verbose level of the output depending on ending command with semicolon
            InterpreterResults.Result  rs = scalaInterpreter.interpret(inputString);

                commandNo = previousCommands.size();
                String answerString = rs.toString();
                GlobalValues.scalalabMainFrame.scalalabConsole.displayText(answerString);
                GlobalValues.lastResultOutputArea.setText(answerString);
                }
   }

         
  private void  makePopup() {
        
   clearErrorConsoleItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
          scalaSciCommands.BasicCommands.cls();
          displayPrompt();
            }
        });

            consolePopup.removeAll();
            consolePopup.add(pasteJMenuItem);
            consolePopup.add(copyJMenuItem);
            consolePopup.add(cutJMenuItem);
            consolePopup.add(helpItem);
            consolePopup.add(resetScalaInterpreterItem);
            consolePopup.add(resetScalaInterpreterEJMLItem);
            consolePopup.add(resetToolboxesScalaInterpreterItem);
            consolePopup.add(clearAllScalaInterpreterItem);
            consolePopup.add(adjustFontItem);
            consolePopup.add(clearConsoleItem);
            consolePopup.add(clearErrorConsoleItem);
            consolePopup.add(displayMatrixItem);
            consolePopup.add(displayOneIndexedMatrixItem);
            consolePopup.add(inspectItem);
            consolePopup.add(clearHistoryItem);
            consolePopup.add(saveHistorItem);
            consolePopup.add(loadHistoryItem);
            consolePopup.add(displayUndockedToolbarItem);
            consolePopup.add(transparentErrorMessagesItem);
            consolePopup.add(verboseErrorMessagesItem);
            
            
  }

      
      
      private class ConsoleHelpAction extends AbstractAction {
               ConsoleHelpAction() {
                   super("Help");
               }
         public void actionPerformed(ActionEvent e) {
          Font  df = new Font("SansSerif", Font.BOLD, 14);
         
          JFrame consHelpFrame = new JFrame("Basic Help");
          JPanel consHelpPanel = new JPanel(new BorderLayout());
          JTextArea consHelpTextArea = new JTextArea();
          consHelpFrame.setLocation(xloc, yloc);
          consHelpFrame.add(consHelpPanel);
          consHelpFrame.setVisible(true);
          Graphics2D gc = (Graphics2D) consHelpPanel.getGraphics();
         FontRenderContext context = gc.getFontRenderContext();
           Rectangle2D bounds = df.getStringBounds(" ", context);
          int mwidth =(int) bounds.getWidth()*150;
          int mheight =(int) bounds.getHeight()*20;
         
           consHelpFrame.setSize(mwidth, mheight);
         
          consHelpTextArea.setFont(df);
          consHelpTextArea.setText(scalaSciCommands.BasicCommands.commands);
          JScrollPane spHelpText = new JScrollPane(consHelpTextArea);
          consHelpPanel.add(spHelpText, BorderLayout.CENTER);
          }
      
      }
      
      
      
      private class InspectAction extends AbstractAction {
               InspectAction() {
                   super("Inspect");
               }
         public void actionPerformed(ActionEvent e) {
          String selectedText = getSelectedText();
          selectedText = selectedText.trim();
          if (selectedText.isEmpty()==false)  {
            Option<String>  typeOfSelected = GlobalValues.bindedVarsTypes.get(selectedText);
            if (typeOfSelected.isDefined())  {
              Option <String> typeOfVar = scalaSciCommands.BasicCommands.typeOf(selectedText);
              String strType = typeOfVar.toString();
              strType = strType.replace("Some(", "");
              strType = strType.replace(")", "");
              
              if ((strType.equalsIgnoreCase("Int")==false) && (strType.equalsIgnoreCase("Double")==false)
                  && (strType.equalsIgnoreCase("Float")==false)
                  && (strType.equalsIgnoreCase("Short")==false)
                  && (strType.equalsIgnoreCase("Char")==false))
              scalaSciCommands.Inspect.inspectg(strType);
            }
           }
         }
      }
         
        
      private class DisplayMatrixAction extends AbstractAction {
               DisplayMatrixAction() {
                   super("Display Matrix");
               }
         public void actionPerformed(ActionEvent e) {
          String selectedText = getSelectedText();
          selectedText = selectedText.trim();
          Option<String>  typeOfSelected = GlobalValues.bindedVarsTypes.get(selectedText);  
          if (selectedText.isEmpty()==false)  {
             if (typeOfSelected.isDefined())  {
                 // construct a script that will display the selected variable
              String dispScript="scalaExec.gui.watchMatrix.display("+selectedText+ ", false)";
              scalaExec.Interpreter.GlobalValues.globalInterpreter.interpret(dispScript);
            }
           }
         }
      }
      
      
     
        private class DisplayMatrixActionOneIndexed extends AbstractAction {
               DisplayMatrixActionOneIndexed() {
                   super("Display one-indexed Matrix");
               }
         public void actionPerformed(ActionEvent e) {
          String selectedText = getSelectedText();
          selectedText = selectedText.trim();
          Option<String>  typeOfSelected = GlobalValues.bindedVarsTypes.get(selectedText);  
          if (selectedText.isEmpty()==false)  {
             if (typeOfSelected.isDefined())  {
                 // construct a script that will display the selected variable
              String dispScript="scalaExec.gui.watchMatrix.display("+selectedText+ ", true)";
              scalaExec.Interpreter.GlobalValues.globalInterpreter.interpret(dispScript);
            }
           }
         }
      }
      
     /**display the previous command in the list*/
	public void prevCommand()
	{
		commandNo--;
    	
    	String text = "";
    	if(commandNo >= 0 && commandNo < previousCommands.size())
    	{
    		text = ((String)previousCommands.elementAt(commandNo)).trim();
    	}
    	else if(commandNo < 0)
    	{
    		text = "";
    		commandNo = -1;
    	}

    	// replace current command with previous command
    	textArea = getText();
        int    pos1     = textArea.lastIndexOf(GlobalValues.scalalabPromptString) + 2;
    	String prev = textArea.substring(0, pos1);
        
    	setText(textArea.substring(0, pos1)+text);
    	
// set cursor at the end of the text area
	setCaretPosition(getText().length());
	}	
	
        /**display the previous command in the list*/
	public void mostRecentCommand()
	{
	String text = ConsoleKeyHandler.prevtext;
    	
        // replace current command with previous command
    	textArea = getText();
        int    pos1     = textArea.lastIndexOf(GlobalValues.scalalabPromptString) + 2;
    	String prev = textArea.substring(0, pos1);
        
    	setText(textArea.substring(0, pos1)+text);
    	
// set cursor at the end of the text area
	setCaretPosition(getText().length());
	}	
	
	/**display the next command in the list*/
	public void nextCommand()
	{
	commandNo++;

    	String text = "";
    	if(commandNo >= 0 && commandNo < previousCommands.size())
    	{
    		text = ((String)previousCommands.elementAt(commandNo)).trim();
    	}
    	else if(commandNo >= previousCommands.size())
    	{
    		text = "";
    		commandNo = previousCommands.size();
    	}		    	

    	// replace current command with next command
    	textArea = getText();
    	int    pos1     = textArea.lastIndexOf(GlobalValues.scalalabPromptString) + 2;
    	setText(textArea.substring(0,pos1)+text);
    	
    	// set cursor at the end of the text area
		setCaretPosition(getText().length());
	}

	
	/**Display the command prompt*/
	public  void displayPrompt()
	{
        if (GlobalValues.displayDirectory==true)     
          append("\n"+GlobalValues.workingDir+GlobalValues.scalalabPromptString);
        else 
          append("\n"+GlobalValues.scalalabPromptString);
        String currentText = getText();
        lineStart = currentText.length();
	setCaretPosition(lineStart);
	}
	

	/** clears the current command line */
	public void clearCommandLine()
	{
	
    	String text = "";
    	
    	// replace current command with next command
    	textArea = getText();
    	int    pos1     = textArea.lastIndexOf(GlobalValues.scalalabPromptString) + 2;
    	setText(textArea.substring(0,pos1)+text);
    	
    	// set cursor at the end of the text area
	setCaretPosition(getText().length());
	}

   
        
           private class MouseAdapterForConsole  extends  MouseAdapter {
        @Override
          public void mousePressed(MouseEvent e) {   
              GlobalValues.userConsole =  (Console) e.getSource();
              xloc = e.getX();
              yloc = e.getY();
              if (e.isPopupTrigger()){  
                consolePopup.show((Component) e.getSource(), e.getX(), e.getY());
             }
           }
    
        @Override
        public void mouseReleased(MouseEvent e) { 
           if (e.isPopupTrigger()){
                 consolePopup.show((Component) e.getSource(), e.getX(), e.getY());
             }       
             
          }
       }
           
           
   
   private    class ConsoleFontAdjusterAction extends AbstractAction  {
        public void actionPerformed(ActionEvent e) {
 JFontChooser  myFontChooser = new JFontChooser(GlobalValues.scalalabMainFrame);
 myFontChooser.setVisible(true);
 Font choosingFont = myFontChooser.getFont();
 Color fontColorChoosed =  myFontChooser.getForegroundColor();
 int rgbAlpha = fontColorChoosed.getAlpha(), rgbRed = fontColorChoosed.getRed(), rgbBlue = fontColorChoosed.getBlue(), rgbGreen = fontColorChoosed.getGreen();
 GlobalValues.scalalabMainFrame.scalalabConsole.setFont(choosingFont);
 GlobalValues.scalalabMainFrame.scalalabConsole.setForeground(myFontChooser.getForegroundColor());
 GlobalValues.settings.setProperty("alphaProp",  Integer.toString(rgbAlpha));
 GlobalValues.settings.setProperty("redProp",  Integer.toString(rgbRed));
 GlobalValues.settings.setProperty("greenProp",  Integer.toString(rgbGreen));
 GlobalValues.settings.setProperty("blueProp",  Integer.toString(rgbBlue));

 int  isBold = 0;   if (choosingFont.isBold()) isBold = 1;
 int  isItalic = 0;   if (choosingFont.isItalic()) isItalic = 1;
 GlobalValues.settings.setProperty("isBoldProp", Integer.toString(isBold));
 GlobalValues.settings.setProperty("isItalicProp", Integer.toString(isItalic));
         }
       }

     
   
    	
}
